package com.cyou.qb.common.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Component;

import com.cyou.nad.dao.PojoPage;
import com.cyou.qb.common.domain.Active;

/**
 * @author zoudongchao
 *
 */
@Component
public class ActiveDao extends MysqlBaseDao {
	/**
	 * 通过活动id获取唯一活动
	 * Description
	 * @author lidongwei
	 * @since 2012-10-6
	 */
	public Active findActiveById(int activeId) {
		return this.getUniqueOne("findActiveById", activeId);
	}
	
	/**
	 * <p>查找所有未删除的已经发布活动  </p>
	 * @author zoudongchao
	 * @return
	 * 		所有未删除的活动
	 */
	public List<Active> finAllActive() {
		return this.getList("findAllActive");
	}

	/**
	 * <p>查找已经结束N天的活动</p>
	 * @author zoudongchao
	 * @param days 结束N天
	 * @return 结束N天的活动列表
	 */
	public List<Active> findEndedActivity(int days) {
		Map<String,Object> params = new HashMap<String, Object>();
		
		Date today = DateUtils.truncate(new Date(), Calendar.DATE);
		Date start = DateUtils.addDays(today, -days);
		Date end = DateUtils.addDays(today, -days+1);
		
		params.put("start", start);
		params.put("end", end);
		return this.getListByEntity("findEndedActivity", params);
	}

	@SuppressWarnings("unchecked")
	public PojoPage<Active> pageQuery(String oderBy,int pageNo, int pageSize) {
		return  this.getPage(oderBy,pageNo, pageSize);
	}
	/**
	 * 保存一个新活动
	 * Description
	 * @author lidongwei
	 * @since 2012-10-6
	 */
	public int saveOneActivity(Active active){
		return this.insert("saveOneActivity", active);
	}
	/**
	 * 修改一个活动
	 * Description
	 * @author lidongwei
	 * @since 2012-10-6
	 */
	public int updateOneActive(Active active){
		return this.update("updateOneActivity", active);
	}

	/**
	 * <p>更新活动抽奖次数</p>
	 * @author zoudongchao
	 * @param id	活动id
	 * @param drawTimes	活动抽奖次数
	 * @return
	 */
	public int updateActiveDrawNum(int id, long drawNum) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("drawNum", drawNum);
		return this.update("updateActiveDrawNum", params);
	}
}
